#include <windows.h>
#include <wx/wx.h>
#include <wx/msw/registry.h>

#include "MyProject4.h"
//#include "MyProject4.cpp"

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
    MainWindows *frame;
    void onclick(wxCommandEvent &event);
    void OnOpenDirDialog(wxCommandEvent &event); 
    void OnAddItemToListEnabled(wxCommandEvent &event);
    void OnReadRegistryPath(wxCommandEvent &event);
    void OnMoveToDisabledList(wxCommandEvent &event);
    void OnMoveToEnabledList(wxCommandEvent &event);
    void OnSaveToRegistry(wxCommandEvent &event);
    void OnSendSettingChange();
    void OnEnabledListDblClick(wxCommandEvent& event);
    void OnDisabledListDblClick(wxCommandEvent& event);
    void OnDeleteSelectedItem(wxCommandEvent& event);
};

void MyApp::OnDeleteSelectedItem(wxCommandEvent& event)
{
    // 获取 m_list_disabled 的选中项索引
    int selection = frame->m_list_disabled->GetSelection();

    if (selection == wxNOT_FOUND)
    {
        // 没有选中项，提示用户选择
        wxMessageBox(wxT("请选择要删除的项。"), wxT("提示"), wxOK | wxICON_INFORMATION);
        return;
    }

    // 删除选中项
    frame->m_list_disabled->Delete(selection);
}
void MyApp::OnDisabledListDblClick(wxCommandEvent& event)
{
    // 获取 m_list_disabled 的选中项索引
    int selection = frame->m_list_disabled->GetSelection();

    if (selection == wxNOT_FOUND)
    {
        // 没有选中项，提示错误
        wxMessageBox(wxT("请选择一个禁用项。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // 获取选中项内容
    wxString selectedItem = frame->m_list_disabled->GetString(selection);

    // 从 disabled 列表中删除该项
    frame->m_list_disabled->Delete(selection);

    // 添加到 enabled 列表的最后
    frame->m_list_enabled->Append(selectedItem);
}
void MyApp::OnEnabledListDblClick(wxCommandEvent& event)
{
    // 获取 m_list_enabled 的选中项索引
    int selection = frame->m_list_enabled->GetSelection();

    if (selection == wxNOT_FOUND)
    {
        // 没有选中项，提示错误
        wxMessageBox(wxT("请选择一个项。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // 获取选中项内容
    wxString selectedItem = frame->m_list_enabled->GetString(selection);

    // 从 enabled 列表中删除该项
    frame->m_list_enabled->Delete(selection);

    // 添加到 disabled 列表的最后
    frame->m_list_disabled->Append(selectedItem);
}
void MyApp::OnSendSettingChange()
{
    // 发送 WM_SETTINGCHANGE 消息，通知系统环境变量已更改
    ::SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", 
                         SMTO_ABORTIFHUNG, 1000, nullptr);

    // wxMessageBox(wxT("WM_SETTINGCHANGE 消息已发送，环境变量已更新。"), 
    //              wxT("提示"), wxOK | wxICON_INFORMATION);
}
void MyApp::OnSaveToRegistry(wxCommandEvent& event)
{
    // 打开注册表路径
    wxRegKey key(wxRegKey::HKLM, wxT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"));

    if (!key.Exists())
    {
        wxMessageBox(wxT("注册表路径不存在。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // ---- 写入 Path ----
    int enabledCount = frame->m_list_enabled->GetCount();
    wxString pathValue;
    wxArrayString pathArray;

    for (int i = 0; i < enabledCount; ++i)
    {
        wxFileName fn=frame->m_list_enabled->GetString(i);
        if(fn.IsOk()){
            pathArray.Add(fn.GetFullPath());
        }
    }
    pathValue = wxJoin(pathArray, ';');
    if (!key.SetValue(wxT("Path"), pathValue))
    {
        wxMessageBox(wxT("无法写入 Path 值。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // ---- 写入 disabledPath ----
    int disabledCount = frame->m_list_disabled->GetCount();
    wxString disabledPathValue;

    for (int i = 0; i < disabledCount; ++i)
    {
        wxString item = frame->m_list_disabled->GetString(i);
        if (!item.IsEmpty())
        {
            if (!disabledPathValue.IsEmpty())
                disabledPathValue += wxT(';');
            disabledPathValue += item;
        }
    }

    if (!key.SetValue(wxT("disabledPath"), disabledPathValue))
    {
        wxMessageBox(wxT("无法写入 disabledPath 值。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    OnSendSettingChange();

    // 提示保存成功
    wxMessageBox(wxT("环境变量已成功保存到注册表。"), wxT("提示"), wxOK | wxICON_INFORMATION);
}
void MyApp::OnMoveToEnabledList(wxCommandEvent& event)
{
    // 获取 m_list_disabled 的选中项索引
    int selection = frame->m_list_disabled->GetSelection();

    if (selection == wxNOT_FOUND)
    {
        // 没有选中项，提示错误
        wxMessageBox(wxT("请选择一个禁用项。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // 获取选中项内容
    wxString selectedItem = frame->m_list_disabled->GetString(selection);

    // 从 disabled 列表中删除该项
    frame->m_list_disabled->Delete(selection);

    // 添加到 enabled 列表的最后
    frame->m_list_enabled->Append(selectedItem);
}
void MyApp::OnMoveToDisabledList(wxCommandEvent& event)
{
    // 获取选中项索引
    int selection = frame->m_list_enabled->GetSelection();

    if (selection == wxNOT_FOUND)
    {
        // 没有选中项，提示错误
        wxMessageBox(wxT("请选择一个项。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // 获取选中项内容
    wxString selectedItem = frame->m_list_enabled->GetString(selection);

    // 从 enabled 列表中删除该项
    frame->m_list_enabled->Delete(selection);

    // 添加到 disabled 列表的最后
    frame->m_list_disabled->Append(selectedItem);
}
void MyApp::OnReadRegistryPath(wxCommandEvent& event)
{
    // 打开注册表路径
    wxRegKey key(wxRegKey::HKLM, wxT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"));

    if (!key.Exists())
    {
        wxMessageBox(wxT("注册表路径不存在。"), wxT("错误"), wxOK | wxICON_ERROR);
        return;
    }

    // ---- 处理 Path ----
    wxString pathValue;
    if (key.QueryValue(wxT("Path"), pathValue))
    {
        frame->m_list_enabled->Clear();

        wxArrayString enabledItems = wxSplit(pathValue, ';');
        for (const wxString& item : enabledItems)
        {
            if (!item.IsEmpty())
            {
                frame->m_list_enabled->Append(item.Strip());
            }
        }
    }
    else
    {
        wxMessageBox(wxT("无法读取 Path 值。"), wxT("警告"), wxOK | wxICON_WARNING);
    }

    // ---- 处理 disabledPath ----
    wxString disabledPathValue;
    if (key.QueryValue(wxT("disabledPath"), disabledPathValue))
    {
        frame->m_list_disabled->Clear();

        wxArrayString disabledItems = wxSplit(disabledPathValue, ';');
        for (const wxString& item : disabledItems)
        {
            if (!item.IsEmpty())
            {
                frame->m_list_disabled->Append(item.Strip());
            }
        }
    }
    else
    {
        // 如果 disabledPath 不存在，则创建一个默认值
        key.SetValue(wxT("disabledPath"), wxEmptyString); // 创建空值
        frame->m_list_disabled->Clear(); // 清空列表
        wxMessageBox(wxT("未找到 disabledPath，已在注册表中创建该键。"), wxT("提示"), wxOK | wxICON_INFORMATION);
    }
    // 提示读取成功
    wxMessageBox(wxT("环境变量已成功从注册表读取。"), wxT("提示"), wxOK | wxICON_INFORMATION);
}
void MyApp::OnAddItemToListEnabled(wxCommandEvent& event)
{
    // 获取文本框内容
    wxString text = frame->m_txt_DirPath->GetValue();

    if (!text.IsEmpty())
    {
        // 添加到 m_list_enabled 的最后
        frame->m_list_enabled->Append(text);

        // 可选：清空文本框
        // frame->m_txt_DirPath->Clear();
    }
}
void MyApp::OnOpenDirDialog(wxCommandEvent& event)
{
    // 创建只选择目录的对话框
    wxDirDialog dirDlg(frame, "请选择一个目录", "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);

    if (dirDlg.ShowModal() == wxID_OK)
    {
        // 获取用户选择的目录路径
        wxString selectedPath = dirDlg.GetPath();

        // 设置到文本框 m_txt_DirPath 中
        frame->m_txt_DirPath->SetValue(selectedPath);
    }
}
void MyApp::onclick(wxCommandEvent &event)
{
    // 获取触发事件的列表框控件
    wxListBox* listBox = dynamic_cast<wxListBox*>(event.GetEventObject());

    if (listBox)
    {
        int selection = listBox->GetSelection();

        if (selection != wxNOT_FOUND && selection > 0) // 确保不是第一个项
        {
            wxString selectedItem = listBox->GetString(selection);

            // 删除当前项
            listBox->Delete(selection);

            // 插入到前一个位置
            listBox->Insert(selectedItem, selection - 1);

            // 保持选中状态
            listBox->SetSelection(selection - 1);
        }
    }
}

bool MyApp::OnInit()
{
    frame = new MainWindows(nullptr, wxID_ANY);
    frame->SetIcon(wxICON(MAINICON));
    frame->SetTitle(wxT("环境变量修改工具1.0.2"));
    // ↓↓↓↓↓逻辑代码↓↓↓↓
    frame->m_btn_FileDialog->Bind(wxEVT_BUTTON, &MyApp::OnOpenDirDialog, this);
    frame->m_btn_add->Bind(wxEVT_BUTTON, &MyApp::OnAddItemToListEnabled, this);
    frame->m_btn_Read_Path->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyApp::OnReadRegistryPath, this);
    frame->m_btn_Down->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyApp::OnMoveToDisabledList, this);
    frame->m_btn_Up->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyApp::OnMoveToEnabledList, this);
    frame->m_btn_Save_Path->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyApp::OnSaveToRegistry, this);
    frame->m_list_enabled->Bind(wxEVT_LISTBOX_DCLICK, &MyApp::OnEnabledListDblClick, this); 
    frame->m_list_disabled->Bind(wxEVT_LISTBOX_DCLICK, &MyApp::OnDisabledListDblClick, this);
    frame->m_btn_Del->Bind(wxEVT_BUTTON, &MyApp::OnDeleteSelectedItem, this);
    // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    frame->Show(true);
    return true;
}

wxIMPLEMENT_APP(MyApp);